package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.MyHorizontalScrollView;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;

/**
 * Created by yinzhiqun on 2015/4/28.
 * 设置体重对话框
 */
public class SetWeightDialog extends Dialog {
    private TextView resultTv;
    private float cell;
    private int currentWeight;
    private TextView weight_number_tv;
    private MyHorizontalScrollView weight_hsv;
    private CommitDialogCallBack callback;

    private int windowWidth;
    private String watchName;
    private Context context;

    /**
     * @param resultTv 修改完成后，显示结果的textview
     */
    public SetWeightDialog(Context context, int currentWeight, TextView resultTv, CommitDialogCallBack callback) {
        super(context);
        this.resultTv = resultTv;
        this.currentWeight = currentWeight;
        this.callback = callback;
    }

    public SetWeightDialog(Context context, int currentWeight, TextView resultTv, String watchName, CommitDialogCallBack callback) {
        super(context);
        this.resultTv = resultTv;
        this.watchName = watchName;
        this.currentWeight = currentWeight;
        this.callback = callback;
        this.context = context;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_setweight);

        setDialogAttributes();
        initDialogView();
    }

    private void initDialogView() {
        cell = transfer(7);//每格是相对7个单位
        //
        weight_number_tv = (TextView) findViewById(R.id.weight_number_tv);
        weight_number_tv.setText(String.valueOf(currentWeight));

        View weight_iv = findViewById(R.id.weight_iv);
        int weightIvPadding = windowWidth / 2 - DensityUtil.dip2px(getContext(), 46);
        weight_iv.setPadding(weightIvPadding, 0, weightIvPadding, 0);
        weight_hsv = (MyHorizontalScrollView) findViewById(R.id.weight_hsv);
        weight_hsv.post(new Runnable() {
            @Override
            public void run() {
                weight_hsv.scrollTo((int) (cell * (currentWeight - 5) + 0.5f), 0);
                weight_hsv.setScrollChangedListener(new MyHorizontalScrollView.ScrollViewListener() {
                    @Override
                    public void onScrollChanged(MyHorizontalScrollView scrollView, int x,
                                                int y, int oldx, int oldy) {
                        if (x != oldx) {
                            currentWeight = convertToWeight(x);
                            weight_number_tv.setText(String.valueOf(currentWeight));
                        }
                    }
                });
            }
        });

        //初始化确定，取消按钮
        findViewById(R.id.cancel_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sure_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    resultTv.setText(currentWeight + "kg");
                    callback.onCommit();
                    dismiss();
                }
                return false;
            }
        });

        /**处理标题*/
        TextView title_name_tv = (TextView) findViewById(R.id.title_name_tv);
        if (watchName != null) {
            MyFrontUtil.initTextViewText(context, title_name_tv, watchName, context.getString(R.string.t_info_weight));
        }
    }


    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        windowWidth = dm.widthPixels;
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }


    /**
     * 相对单位转绝对像素
     *
     * @param source
     * @return
     */
    private float transfer(float source) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return source * scale;
    }

    private int convertToWeight(int x) {
        return (int) (x / cell + 0.5f) + 5;
    }

}
